#!/bin/bash
function install_nginx() {
    echo "安装Nginx" >>$logs_file
    nginxPid=`pidof nginx`
    if [ -n "$nginxPid" ];then
        whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "系统已存在Nginx，请删除后继续！！！" 10 70
        sleep 0.15
        return 1
    fi
    rm -rf /etc/rc.d/init.d/nginx >>/dev/null 2>&1
    rm -rf /etc/systemd/system/nginx.service >>/dev/null 2>&1
    
    nginxVer=$(whiptail --backtitle "Author: Elvis" --title "NGINX安装配置" --inputbox \
        "输入需要安装的Nginx版本，如果本地不存在，则需要联网下载" 10 60 1.18.0 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    
    if [ -z "$nginxVer" ];then
        nginxVer="1.18.0"
    fi
    nginxVer=`echo $nginxVer`
    
    if [ ! -f $workHome/packages/nginx/nginx-$nginxVer.tar.gz -a "$die_network" == "ture" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统未联网，且本地不存在需要安装的Nginx版本！！！" 10 70
        sleep 0.15
        return 1
    fi
    
    nginxPath=$(whiptail --backtitle "Author: Elvis" --title "NGINX安装配置" --inputbox \
        "输入Nginx安装位置" 10 60 "/usr/local/nginx" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    nginxPath=${nginxPath:-/usr/local/nginx}
    nginxPath=`echo $nginxPath`
    nginxPath=${nginxPath%/}
    isNull=`ls $nginxPath/* 2>/dev/null | wc -l`
    if [[ $isNull -gt 0 ]];then
        whiptail --backtitle "Author: Elvis" --title "警告" --yes-button "继续" --no-button "退出" --yesno "给出的Nginx安装目录非空，继续安装会删除已有数据！\n\n是否继续安装？" 10 60
        if [ "$?" != "0" ];then
         sleep 0.10
         return 1        
        fi
    fi
    
    command -v ss >/dev/null 2>&1
    if [ "$?" == "0" ];then
        listen_cmd="ss -lnpt"
    else
        command -v netstat >/dev/null 2>&1
        if [ "$?" == "0" ];then
          listen_cmd="netstat -tnlp"
        fi
    fi
    [[ $listen_cmd ]] && PortCheck=Yes || PortCheck=No
    PortUsed=
    PortUsed_Pro=
    nginxPort=
    while [[ ! "$nginxPort" =~ ^[1-9][0-9]+$ || "$PortUsed" == "Yes" ]]
    do
        nginxPort=$(whiptail --backtitle "Author: Elvis" --title "Nexus配置" --inputbox --nocancel \
        "请输入Nginx监听端口，注意端口是否被占用" 10 60 "80" 3>&1 1>&2 2>&3)
        if [[ ! "$nginxPort" =~ ^[1-9][0-9]+$ ]];then
             whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Nginx端口只能为大于两位的数字，且不能以0开头，请重新输入！" 10 60
             sleep 0.5
        else
            if [ "$PortCheck" == "Yes" ];then
                PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${nginxPort} "`
                if [ -n "$PortUsed_Pro" ];then
                  PortUsed=Yes
                  whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Nginx端口已被使用，请重新输入！" 8 60
                else
                  PortUsed=No
                fi
            else
                PortUsed=No
            fi
        fi
    done
    nginxPort=${nginxPort:-80}
    nginxPort=`echo $nginxPort`
    
    whiptail --backtitle "Author: Elvis" --title "Nginx安装信息汇总" --yesno "\nNginx版本：$nginxVer\nNginx安装位置：$nginxPath\nNginx端口：$nginxPort\n\nYes以继续安装，No退出Nginx安装" 15 70
    if [ "$?" != "0" ];then
        sleep 0.10
        return 1
    fi
    nginxHome="$workHome/packages/nginx"
    cd $nginxHome
    {
    echo 1
    if [ "$die_network" == "true" ];then
        ping -c 2 -w 5 180.76.76.76 >/dev/null 2>&1
        if [ "$?" != "0" ];then
           die_network="true"
        else
           die_network="false"
        fi
    else
        sleep 1
    fi
    if [[ ! -f ${nginxHome}/nginx-${nginxVer}.tar.gz && "$die_network" == "false" ]];then
        msg="下载nginx安装包"
        echo $msg >>$logs_file
        echo -e "XXX\n3\n$msg\nXXX"
        echo 3
        echo "首先从华为云下载，失败后尝试从官方下载" >>$logs_file
        echo -e "XXX\n5\n下载nginx安装包中......\nXXX"
        echo 5
        curl --progress-bar --connect-timeout 30 -m 600 -O --fail https://repo.huaweicloud.com/nginx/nginx-${nginxVer}.tar.gz >>$logs_file 2>&1
        tar zxf nginx-${nginxVer}.tar.gz -C /tmp/>/dev/null 2>&1
        download_status=$?
        if [ "${download_status}" != "0" ];then
            rm -rf /tmp/nginx-${nginxVer}
            rm -rf nginx-${nginxVer}.tar.gz
            sleep 1
            echo "">>$logs_file
            curl --progress-bar --connect-timeout 30 -m 600 --fail -O http://nginx.org/download/nginx-${nginxVer}.tar.gz >>$logs_file 2>&1
            download_status=$?
        else
            rm -rf /tmp/nginx-${nginxVer}
        fi
                
        if [ ${download_status} -ne 0 ];then
           msg="下载nginx失败，请检查网络或者不存在此版本的Nginx！！！"
           echo $msg >>$logs_file
           echo -e "XXX\n8\n$msg\nXXX"
           echo 8
           echo "$msg" >/tmp/.nginx.err.info
           sleep 3
           return 1
        fi
    elif [[ ! -f ${nginxHome}/nginx-${nginxVer}.tar.gz && "$die_network" == "true" ]];then
        msg="你的主机未联网，请提前手动下载安装包，使用离线安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n8\n$msg\nXXX"
        echo 8
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi

    msg="安装所需基础RPM包"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n10\n$msg\nXXX"
    echo 10
    yum -y install make gcc-c++ cmake bison-devel ncurses-devel gcc perl wget unzip bison \
    glibc glibc-devel openssl-devel openssl libaio-devel autoconf automake  libxml*  libtool-ltdl-devel* >>$logs_file 2>&1
    if [ "$?" != "0" ];then
        msg="基础依赖软件失败，退出安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n15\n$msg\nXXX"
        echo 15
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    
    for i in make gcc gcc-c++ cmake openssl openssl-devel glibc glibc-devel automake perl autoconf
    do
        rpm -q $i >/dev/null 2>&1
        if [ "$?" != "0" ];then
            msg="基础依赖软件${i}安装失败，退出安装！！！"
            echo $msg >>$logs_file
            echo -e "XXX\n15\n$msg\nXXX"
            echo 15
            echo "$msg" >/tmp/.nginx.err.info
            sleep 3
            return 1
        fi
    done
    
    find $nginxHome/* -type d|xargs rm -rf
    echo "注意：以下编译安装过程，仅输出错误信息，编译日志存放在/tmp/nginx-comp.log" >>$logs_file
    msg="安装zlib"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n20\n$msg\nXXX"
    echo 20
    echo "$msg" >/tmp/nginx-comp.log
    cd $nginxHome
    zlibPack=`ls -r |grep zlib|head -n 1`
    if [ -z "$zlibPack" ];then
        msg="下载zlib"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n22\n$msg\nXXX"
        echo 22
        curl --connect-timeout 30 -m 360 --fail -O http://www.zlib.net/zlib-1.2.11.tar.gz >>$logs_file 2>&1
        tar zxf zlib-1.2.11.tar.gz >/dev/null 2>&1
        download_status=$?
        rm -rf zlib-1.2.11
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf zlib-1.2.11.tar.gz
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974549/download/zlib-1.2.11.tar.gz >>$logs_file 2>&1
        tar zxf zlib-1.2.11.tar.gz >/dev/null 2>&1
        download_status=$?
        rm -rf zlib-1.2.11
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="zlib下载失败，退出安装！！！"
        rm -rf zlib-1.2.11.tar.gz
        echo $msg >>$logs_file
        echo -e "XXX\n25\n$msg\nXXX"
        echo 25
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    
    zlibPack=`ls -r |grep zlib|head -n 1`
    zlibuPack=${zlibPack%.tar*}
    echo $zlibPack|grep ".tar.gz">/dev/null && tar zxf $zlibPack >/dev/null 2>>$logs_file ||tar jxf $zlibPack >/dev/null 2>>$logs_file
    cd $zlibuPack && ./configure --prefix=/usr/local/zlib >>/tmp/nginx-comp.log 2>>$logs_file&& \
    make -j8 >>/tmp/nginx-comp.log 2>>$logs_file&& make install >>/tmp/nginx-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装zlib失败，退出安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n25\n$msg\nXXX"
        echo 25
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/nginx-comp.log
    
    msg="安装openssl"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    echo "$msg">>/tmp/nginx-comp.log
    cd $nginxHome
    opensslPack=`ls -r |grep openssl|head -n 1`
    if [ -z "$opensslPack" ];then
        msg="下载openssl"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n32\n$msg\nXXX"
        echo 32
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974553/download/openssl-1.1.1k.tar.gz >>$logs_file 2>&1
        tar zxf openssl-1.1.1k.tar.gz >/dev/null 2>&1
        download_status=$?
        rm -rf openssl-1.1.1k
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf openssl-1.1.1k.tar.gz
        curl --connect-timeout 30 -m 360 --fail -O https://www.openssl.org/source/openssl-1.1.1k.tar.gz >>$logs_file 2>&1
        tar zxf openssl-1.1.1k.tar.gz >/dev/null 2>&1
        download_status=$?
        rm -rf openssl-1.1.1k
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="openssl下载失败，退出安装！！！"
        rm -rf openssl-1.1.1k.tar.gz
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n35\n$msg\nXXX"
        echo 35
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    
    opensslPack=`ls -r |grep openssl|head -n 1`
    openssluPack=${opensslPack%.tar*}
    echo $opensslPack|grep ".tar.gz">/dev/null && tar zxf $opensslPack >/dev/null 2>>$logs_file ||tar jxf $opensslPack >/dev/null 2>>$logs_file
    cd $openssluPack && echo -e "XXX\n32\n预编译openssl中......\nXXX" && echo 32 &&./config --prefix=/usr/local/openssl >>/tmp/nginx-comp.log 2>>$logs_file && \
    echo -e "XXX\n33\n编译openssl中......\nXXX" && echo 33 && make -j8>>/tmp/nginx-comp.log 2>>$logs_file &&\
    echo -e "XXX\n34\n安装openssl中......\nXXX" && echo 34 && make install >>/tmp/nginx-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装openssl失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n35\n$msg\nXXX"
        echo 35
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/nginx-comp.log
    
    msg="安装pcre"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    echo "$msg" >>/tmp/nginx-comp.log
    cd $nginxHome
    pcrePack=`ls -r |grep pcre|head -n 1`
    apPcrePack=`ls -r $workHome/packages/apache/ 2>/dev/null|grep pcre|head -n 1`
    if [ -z "$pcrePack" ];then
        if [ -z "$apPcrePack" ];then
           msg="下载PCRE"
           echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
           echo -e "XXX\n42\n$msg\nXXX"
           echo 42
           curl --connect-timeout 30 -m 360 --fail -O https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.bz2 >>$logs_file 2>&1
           tar jxf pcre-8.44.tar.bz2 >/dev/null 2>&1
           download_status=$?
           rm -rf pcre-8.44
        else
           msg="从Apache安装目录复制Pcre"
           echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
           echo -e "XXX\n42\n$msg\nXX"
           echo 42
           /bin/cp -f $workHome/packages/apache/$ngPcrePack . >>$logs_file 2>&1
           download_status=$?
        fi
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf pcre-8.44.tar.bz2
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974552/download/pcre-8.44.tar.bz2 >>$logs_file 2>&1
        tar jxf pcre-8.44.tar.bz2 >/dev/null 2>&1
        download_status=$?
        rm -rf pcre-8.44
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="pcre下载失败，退出安装！！！"
        rm -rf pcre-8.44.tar.bz2
        echo $msg >>$logs_file
        echo -e "XXX\n45\n$msg\nXXX"
        echo 45
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    
    pcrePack=`ls -r |grep pcre|head -n 1`
    pcreuPack=${pcrePack%.tar*}
    echo $pcrePack|grep ".tar.gz">/dev/null && tar zxf $pcrePack >/dev/null 2>>$logs_file ||tar jxf $pcrePack >/dev/null 2>>$logs_file
    cd $pcreuPack && echo -e "XXX\n41\n预编译pcre中.....\nXXX"&&echo 41 && ./configure --prefix=/usr/local/pcre >>/tmp/nginx-comp.log 2>>$logs_file&& \
    echo -e "XXX\n42\n编译pcre中......\nXXX" && echo 42 && make -j8 >>/tmp/nginx-comp.log 2>>$logs_file && \
    echo -e "XXX\n44\n安装pcre中......\nXXX" && echo 44 && make install >>/tmp/nginx-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装pcre失败，退出安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n45\n$msg\nXXX"
        echo 45
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/nginx-comp.log
    
    msg="安装nginx"
    rm -rf $nginxPath
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n50\n$msg\nXXX"
    echo 50
    echo "$msg" >>/tmp/nginx-comp.log
    cd $nginxHome
    tar zxf nginx-${nginxVer}.tar.gz >/dev/null 2>>$logs_file
    cd nginx-${nginxVer} && \
    echo -e "XXX\n51\n预编译Nginx中......\nXXX" && echo 51 && ./configure --prefix=$nginxPath --with-http_stub_status_module --with-http_ssl_module --with-http_gunzip_module --with-http_gzip_static_module --with-pcre=$nginxHome/$pcreuPack --with-zlib=$nginxHome/$zlibuPack >>/tmp/nginx-comp.log 2>>$logs_file && \
    echo -e "XXX\n53\n编译Nginx中......\nXXX" && echo 53 && make -j8 >>/tmp/nginx-comp.log 2>>$logs_file && \
    echo -e "XXX\n54\n安装Nginx中......\nXXX" && echo 54 && make install >>/tmp/nginx-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装nginx失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n55\n$msg\nXXX"
        echo 55
        echo "$msg" >/tmp/.nginx.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/nginx-comp.log
    
    msg="配置nginx服务脚本"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n60\n$msg\nXXX"
    echo 60
    sleep 1
    if [ "$system_ver" == "6" ];then
        cat > /etc/rc.d/init.d/nginx <<'EOF'
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: $nginxPath/conf/nginx.conf
nginxd=$nginxPath/sbin/nginx
nginx_config=$nginxPath/conf/nginx.conf
nginx_pid=$nginxPath/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
command -v ss >/dev/null 2>&1
if [ "$?" == "0" ];then
    listen_cmd="ss"
else
    command -v netstat >/dev/null 2>&1
    if [ "$?" == "0" ];then
        listen_cmd="netstat"
    fi
fi
start() {
if [ -e $nginx_pid ];then
$listen_cmd -lanpt |grep nginx >/dev/null 2>&1
   if [ "$?" == "0" ];then
   echo "nginx already running...."
   exit 1
   fi
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginxPath/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
EOF
    sed -i "s#\$nginxPath#$nginxPath#g" /etc/rc.d/init.d/nginx
    chmod +x /etc/rc.d/init.d/nginx
    chkconfig --add nginx >/dev/null 2>&1
    else
        cat >/etc/systemd/system/nginx.service<<'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
Type=forking
PIDFile=$nginxPath/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f $nginxPath/logs/nginx.pid
ExecStartPre=$nginxPath/sbin/nginx -t
ExecStart=$nginxPath/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF
    sed -i "s#\$nginxPath#$nginxPath#g" /etc/systemd/system/nginx.service >>$logs_file 2>&1
    systemctl daemon-reload >/dev/null 2>&1
    fi
    
    msg="修改Nginx配置"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n65\n$msg\nXXX"
    echo 65
    sed -i '/pass the PHP scripts/{:a;n;s/#//g;/\}/!ba}' $nginxPath/conf/nginx.conf >>$logs_file 2>&1
    sed -i 's/\(fastcgi_param  SCRIPT_FILENAME\).*/\1 \$document_root\$fastcgi_script_name;/' $nginxPath/conf/nginx.conf >>$logs_file 2>&1
    sed -i "s/^[[:blank:]]*listen.*80;/        listen $nginxPort;/g" $nginxPath/conf/nginx.conf >>$logs_file 2>&1
    #添加phpinfo.php
    echo "<?php" >>$nginxPath/html/phpinfo.php
    echo "phpinfo();" >>$nginxPath/html/phpinfo.php
    echo "?>" >>$nginxPath/html/phpinfo.php
    echo  "请使用浏览器访问\033[0m http://$ipaddr/phpinfo.php" >>$logs_file
    
    msg="启动Nginx"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    sleep 1
    killall -9 nginx>/dev/null 2>&1
    service nginx start>>$logs_file 2>/dev/null||systemctl start nginx>>$logs_file 2>/dev/null
    
    msg="设置Nginx随系统启动"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    sleep 1
    chkconfig --level 2345 nginx on >>$logs_file 2>/dev/null||systemctl enable nginx>>$logs_file 2>/dev/null
    echo 90
    sleep 1
    echo -e "XXX\n100\ndone！！！\nXXX"
    echo 100
    echo "`date "+%Y-%m-%d %H:%M:%S"` nginx安装完成" >>$logs_file
    echo " ">>$logs_file
    } | whiptail --backtitle "Author: Elvis" --title "安装Nginx服务" --gauge " " 6 60 0
    if [ ! -f /etc/systemd/system/nginx.service -a ! -f /etc/rc.d/init.d/nginx ];then
        errInfo=`cat /tmp/.nginx.err.info`
        rm -rf /tmp/.nginx.err.info
        msg="nginx安装失败！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg\n\n$errInfo" 12 60
        sleep 0.15
        return 1
    fi

    echo "检查nginx服务是否运行正常" >>$logs_file
    nginxPid=`pidof nginx`
    if [ -z "$nginxPid" ];then
        $nginxPath/sbin/nginx >/dev/null 2>&1
        sleep 1
        nginxPid=`pidof nginx`
        if [ -z "$nginxPid" ];then
          msg="nginx启动失败，可能为安装有问题或者端口被占用！！！"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg" 10 60
        else
          msg="nginx服务脚本存在问题，无法控制nginx服务启停，请检查服务脚本！！！"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "$msg" 10 60
        fi
    else
        nginxVer=`$nginxPath/sbin/nginx -v 2>&1|awk -F"/" '{print $2}'`
        msg="nginx $nginxVer安装完成，并且服务运行正常\nNginx安装位置：$nginxPath"
        [[ "$nginxPort" = "80" ]] && requestURL="http://$ipaddr" || requestURL="http://$ipaddr:$nginxPort"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg，请访问$requestURL，检验nginx是否运行正常。" >>$logs_file
        echo " ">>$logs_file
        whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg\n\
        你可以通过使用service或systemctl命令来控制nginx启停\n\n提示：\n请访问$requestURL，检验nginx是否运行正常" 13 60
    fi
}